if (typeof window.RIKAI_getTextAtCarrot == 'undefined') {
  window.onerror = function(msg,url,ln) {
    window.RIKAI.log("javascript error:" + ln + ":" + msg );
    return true;
  };
  window.RIKAI_getTextAtCarrot = function(x,y,androidx,androidy) {
    var i;
    var range = document.caretRangeFromPoint(x, y);
    for (i=0; (!range || range.startOffset == range.startContainer.data.length) && i< 60; i = i + 5) {
      range = document.caretRangeFromPoint(x + i, y);
    }
  
    if (range) {
      var rp = range.startContainer;
      var ro = range.startOffset;
      if ((rp.data) && ro == rp.data.length) {
        if ((rp.data) && (rp.nextSibling.nodeName == 'WBR')) {
          rp = rp.nextSibling.nextSibling;
          ro = 0;
        } else {
          rp = rp.parentNode.nextSibling;
          ro = 0;
        }
      }
      if (ro == 1 && i > 0) {
        ro = 0;
        range.setStartBefore(rp);
      }
      if (range.startContainer.nodeType == 3 && range.startContainer.length > range.startOffset + 20) {
        range.setEnd(range.startContainer, range.startOffset + 20);
      } else {
        range.setEndAfter(range.endContainer.parentNode);
        if (range.endContainer == range.startContainer) {
          if (range.endOffset > range.startOffset + 20) {
            range.setEnd(range.endContainer, range.startOffset + 20);
          }
        }   
      }
      var txt = range.toString();
      if (txt.length > 20) {
        txt = txt.substring(0,20);
      }
      var selection = window.getSelection();
      selection.removeAllRanges();
      range.setEnd(range.startContainer,ro+1);
      selection.addRange(range);
  
      RIKAI.registerText(androidx,androidy,txt);
    }
  };
  var extraCss = document.createElement("b");
  extraCss.innerHTML = "<style type=\"text/css\">::selection { background:#C00;color:#FFF; }</style>";
  document.body.appendChild(extraCss);

  window.RIKAI_insert_css_once = function(tag, css) {
    var mycss = window.document.getElementById(tag);
    if (typeof mycss != "undefined") {
      var head = window.document.getElementsByTagName("head")[0];
      if (typeof head != "undefined") {
        var extraCss = window.document.createElement("b");
        extraCss.innerHTML = "<style type=\"text/css\" id=\"" + tag + "\">\n" + css + "\n</style>";
        head.appendChild(extraCss);
      }
    }
  };

  window.RIKAI_replaceAds = function() {
    return;
    var imgs = window.document.getElementsByTagName("img");
    for (var i=0; i<imgs.length; i+=1) {
      imgs[i].style.visibility="hidden";
      if (typeof imgs[i].src == 'undefined') { imgs[i].src=""; }
      imgs[i].parentNode.removeChild(imgs[i]);
    }
  };
  window.RIKAI_registerLinkTitles = function() {
    var looper;
    for (looper = 0; looper < window.document.links.length; looper++) {
      var addEvents = function(a) {
        a.addEventListener("mousemove",function() { RIKAI.registerTitle(a.text, a.href); return true; });
        a.addEventListener("mousedown",function() { RIKAI.registerTitle(a.text, a.href); return true; });
        a.addEventListener("mouseup",function() { RIKAI.registerTitle(a.text, a.href); return true; });
      };
      addEvents(document.links[looper]);
    }
  };
} 

try { if (window.RIKAI_replaceAds!=null) { window.RIKAI_replaceAds(); } } catch (errorvariable) { window.RIKAI.log("replaceAds error:" + errorvariable.description); }
try { RIKAI.registerTitle(document.title, document.URL); } catch (errorvariable) { window.RIKAI.log("registerTitle error:" + errorvariable.description); }
try { if (window.RIKAI_registerLinkTitles != null) { window.RIKAI_registerLinkTitles(); } } catch (errorvariable) { window.RIKAI.log("registerLinkTitles error:" + errorvariable.description); }

